//
//  MapViewController.m
//  test
//
//  Created by Ruj on 11/17/13.
//  Copyright (c) 2013 Ruj. All rights reserved.
//

#import "MapViewController.h"
#import "Database.h"
#import "ColorMKPolyLine.h"
#import "DriveData.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize recordID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    // Need to be done explicitly not by NIB
    self.mapView.delegate = self;
    
    // Loading database
    Database* db = [Database getSharedInstance];
    
    NSMutableArray* data = [db getDriveRecordDetail:recordID];
    
    // So something if we have data
    if(data.count > 0){
        DriveData* first = [data firstObject];
        DriveData* last = [data lastObject];
        
        // Center and zoom appropriately
        CLLocationCoordinate2D start =
        CLLocationCoordinate2DMake(first->latitude,first->longtitude);
        
        MKCoordinateSpan span = MKCoordinateSpanMake(abs(first->latitude - last->latitude),
                                                     abs(first->longtitude - last->longtitude));
        self.mapView.region = [self.mapView regionThatFits:MKCoordinateRegionMake(start, span)];
        
        // Create map overlay
        MKPointAnnotation* ann = [MKPointAnnotation new];
        ann.coordinate = start;
        ann.title = @"Start";
        [self.mapView addAnnotation:ann];
        ann = [MKPointAnnotation new];
        ann.coordinate = CLLocationCoordinate2DMake(last->latitude,last->longtitude);
        ann.title = @"Stop";
        [self.mapView addAnnotation:ann];
        
        // Drop first object
        [data removeObjectAtIndex:0];
        
        CLLocationCoordinate2D coorPair[2];
        coorPair[0] = start;
        for (DriveData *driveData in data) {
            coorPair[1] = CLLocationCoordinate2DMake(driveData->latitude,
                                                     driveData->longtitude);
            
            ColorMKPolyline *line = [ColorMKPolyline new];
            line.polyline =  [MKPolyline polylineWithCoordinates:coorPair count:2];
            line.color = [UIColor colorWithRed:1 - (1 - driveData->force) green:(1 - driveData->force) blue:0 alpha:1];
            
            
            // Switch later coordinate to the beginning
            coorPair[0] = coorPair[1];
            
            [self.mapView addOverlay:line];
        }
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Delegate for MapView

// For handling overlay
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay{
    
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.strokeColor = [UIColor cyanColor];
        routeRenderer.lineWidth = 5.0;
        
        return routeRenderer;
    }
    else if ([overlay isKindOfClass:[ColorMKPolyline class]]) {
        ColorMKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route.polyline];
        routeRenderer.strokeColor = route.color;
        routeRenderer.lineWidth = 5.0;
        
        return routeRenderer;
    }
    return nil;
    
}

@end
